#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QTimer>
#include<QPainter>
#include"plant.h"
#include<QKeyEvent>
//#include<QDebug>
#include<QSound>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    initScene();
    gamestart();
    this->grabKeyboard();
    this->setFocusPolicy(Qt::ClickFocus);
}
void MainWindow::initScene(){
    setFixedSize(900, 600);
    setWindowTitle("cyberplants");

    //定时器设置
    m_Timer.setInterval(20);
    zom_Timer.setInterval(5);
    bullet_Timer.setInterval(1);
    //僵尸出场时间间隔初始化
    m_recorder = 0;
    //近战时间初始化
    d_recorder=0;
}
//绘制背景的实现
void MainWindow::paintEvent(QPaintEvent *){
    QPainter painter(this);
//<<<<<<< HEAD
//       QPixmap pix;
//        pix.load(":/D:/download/cpp2021-pvz-master/cpp2021-pvz-master/PVZ/images/Background.jpg");
//        painter.drawPixmap(0,0,pix);
//    //画植物栏
//    painter.drawPixmap(200, 0, 500, 100, QPixmap(":/D:/download/cpp2021-pvz-master/cpp2021-pvz-master/PVZ/images/Shop.png"));
//    //画铲铲
//    painter.drawPixmap(710, 0, 100, 100, QPixmap(":/D:/download/cpp2021-pvz-master/cpp2021-pvz-master/PVZ/images/ShovelBank.png"));
//    painter.drawPixmap(710, 0, 100, 100, QPixmap(":/D:/download/cpp2021-pvz-master/cpp2021-pvz-master/PVZ/images/Shovel.png"));
//    //画子弹

    painter.drawPixmap(m_map.m_map1_posX,0,m_map.m_map1);
    painter.drawPixmap(50,0,m_map.m_map2);
    painter.drawPixmap(75,25,m_map.m_map3);
//>>>>>>> a05479c (第六次提交，代码基本完成)
    for(int i=0;i<30;i++){
        //如果非空闲，绘制
        if(temp_plant.m_bullets[i].m_free==false){
            painter.drawPixmap(temp_plant.m_bullets[i].m_x, temp_plant.m_bullets[i].m_y,temp_plant.m_bullets[i].m_Bullet);
        }
    }
//<<<<<<< HEAD

//=======
    //绘制僵尸
    for(int i=0;i<ENEMY_NUM;i++){
        if(m_enemys[i].m_free==false){
            painter.drawPixmap(m_enemys[i].m_x,m_enemys[i].m_y,m_enemys[i].m_enemy);
        }
    }
    //战败画面
    if(def>=10){

    }
//>>>>>>> a05479c (第六次提交，代码基本完成)

    //画植物
    painter.drawPixmap(temp_plant.m_x,temp_plant.m_y,temp_plant.m_plant);
    //画近战
    if(m_kill.m_free==false)//如果近战非空闲则绘制
    {
        painter.drawPixmap(m_kill.m_x+50,m_kill.m_y+50,m_kill.m_kill);
        //qDebug()<<"KillBeingDraw";
    }
}
void MainWindow::gamestart() {
    //启动背景音乐
    QSound::play(":/PVZ/images/55--Graze the Roof.wav");
    //启动定时器
    m_Timer.start();
    connect(&m_Timer,&QTimer::timeout,[=](){
        //更新游戏中元素的坐标业务员q
        updatePosition();
        show_sum();
        //碰撞检测
        collisionDetection();
        //绘制到屏幕中
        update();


    });
    zom_Timer.start();
    connect(&zom_Timer,&QTimer::timeout,[=](){

        //僵尸出场
        enemyToScene();;
    });

}
void MainWindow::updatePosition() {
    //发射子弹
    temp_plant.shoot();
//<<<<<<< HEAD
//=======
    //更新地图
    m_map.mapPosition();

//>>>>>>> a05479c (第六次提交，代码基本完成)
    //计算所有非空闲子弹的当前坐标
    for(int i=0;i<30;i++){
        //如果非空闲，计算发射位置
        if(temp_plant.m_bullets[i].m_free==false){
            temp_plant.m_bullets[i].updatePosition();
        }
    }
    for(int i=0;i<ENEMY_NUM;i++){
        if(m_enemys[i].m_free==false){
            m_enemys[i].updatePosition();
        }
    }
//<<<<<<< HEAD
    //测试子弹
    //temp_Bullet.m_free = false;
    //temp_Bullet.updatePosition();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event){
    int x=event->x();
    int y=event->y();
    temp_plant.setPosition(x-25,y-25);
    //边界检测
    if(x<=150){
        x=150;
    }
    if(x>=875){
        x=875;
    }
    if(y<=100){
        y=100;
    }
    if(y>=400){
        y=400;
    }

}
void MainWindow::show_sun_sum(){

    //僵尸出场_
    for(int i=0;i<ENEMY_NUM;i++){
        if(m_enemys[i].m_free==false){
            m_enemys[i].updatePosition();

        }
    }

}


void MainWindow::enemyToScene(){
    m_recorder++;
    //未达到出场间隔
    if(m_recorder<ENEMY_INTERVAL){
        return;
    }
    m_recorder=0;
    for(int i=0;i<ENEMY_NUM;i++){
        //如果空闲，僵尸出场
        if(m_enemys[i].m_free){
            m_enemys[i].m_free=false;
            //坐标
            m_enemys[i].m_x=900;
            m_enemys[i].m_y= 50 + rand() % (500 - 50 + 1);;
            break;
        }
    }
}
void MainWindow::collisionDetection(){
    //遍历所有非空闲的僵尸
    for(int i=0;i<ENEMY_NUM;i++){
        if(m_enemys[i].m_free){
            continue;
        }

     //遍历所有子弹
        for(int j=0;j<Bullet_Num;j++){
            if(temp_plant.m_bullets[j].m_free){
                continue;
        }
      //如果子弹和僵尸相交，发生碰撞，同时将飞机和子弹空闲状态设置为真
            if(m_enemys[i].m_Rect.intersects(temp_plant.m_bullets[j].m_Rect)){
                m_enemys[i].m_free=true;
                temp_plant.m_bullets[j].m_free=true;
                sum++;
                //QSound::play(":/PVZ/images/pea.wav");
            }
      //近战检测
        }
        if(m_enemys[i].m_Rect.intersects(m_kill.m_Rect)){
            m_enemys[i].m_free=true;
            sum++;
        }

    }
}
//void MainWindow::mouseMoveEvent(QMouseEvent *event_1){
//    int x=event_1->x();
//    int y=event_1->y();
//    temp_plant.setPosition(x-25,y-25);
//    //边界检测
//    if(x<=150){
//        x=150;
//    }
//    if(x>=875){
//        x=875;
//    }
//    if(y<=100){
//        y=100;
//    }
//    if(y>=400){
//        y=400;
//    }

//}
void MainWindow::keyPressEvent(QKeyEvent *ev){
    if(ev->key() == Qt::Key_E){
       m_kill.m_free=false;
       m_kill.m_x=temp_plant.m_x+15;
       m_kill.m_y=temp_plant.m_y-60;
        //qDebug()<<"ReleaseShift";
    }
    if(ev->key() == Qt::Key_Q){

            //qDebug()<<"ReleaseShift";
        }
}
void MainWindow::keyReleaseEvent(QKeyEvent *ev){
    if(ev->key() == Qt::Key_E){
       m_kill.m_free=true;
       m_kill.m_x=1000;
       m_kill.m_y=1000;
       //播放音效
       QSound::play(":/PVZ/images/punch.wav");
        //qDebug()<<"ReleaseShift";
    }

}
void MainWindow::show_sum()
{
    label_sun->setGeometry(100, 110, 60, 20);
    QFont qfont;
    qfont.setPointSize(15);
    label_sun->setFont(qfont);
    label_sun->setNum(sum);
    label_sun->show();
}
void MainWindow::defeated(){
    for(int i=0;i<ENEMY_NUM;i++){
        if(m_enemys[i].m_x<=100){
            m_enemys[i].m_free=true;
            sum--;
        }
    }
}
MainWindow::~MainWindow()
{
    delete ui;
}

